﻿using StarPizza.Model.BaseModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.ClientSystem
{
    public class ClientVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        Client client;
        public Client WrappedClient { get { return client; } }
        public ClientVM(Client client)
        {
            this.client = client;
            client.PropertyChanged += client_PropertyChanged;
            this.IsAllowed = client.IsAllowed;
        }

        //Forward accessor
        public int ID 
        {
            get { return client.BaseUser.ID; }
        }
        public string Login
        {
            get { return client.Login; }
        }
        //Forward accessors and implicit INotifyPropertyChanged handling
        public string Name
        {
            get { return client.Name; }
            set { client.Name = value; }
        }
        //Forward accessors and implicit INotifyPropertyChanged handling
        public int District
        {
            get { return client.District; }
            set { client.District = value; }
        }
        //Forward accessors and implicit INotifyPropertyChanged handling
        public string Address
        {
            get { return client.Address; }
            set { client.Address = value; }
        }

        void client_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Login":
                case "Name":
                case "District":
                case "Address":
                    OnPropertyChanged(e.PropertyName);
                    break;
                case "IsAllowed":
                    IsAllowed = client.IsAllowed;
                    break;
            }
        }

        private bool isAllowed;
        public bool IsAllowed
        {
            get { return isAllowed; }
            set
            {
                if (value != isAllowed)
                {
                    isAllowed = value;
                    OnPropertyChanged("IsAllowed");
                }
            }
        }
        public void UpdateModel()
        {
            client.IsAllowed = this.IsAllowed;
        }
    }
}
